package com.example.service;

import com.example.mapper.StudentMapper;
import com.example.mapper.TeacherMapper;
import com.example.model.Student;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import com.example.model.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentService {
    public StudentService() {
    }

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    public String register(Student student) {
        Student studentResult = studentMapper.findByNumber(student.getNumber());
        Teacher teacher = teacherMapper.findBynumber(student.getNumber());
        if(studentResult==null && teacher==null) {
            studentMapper.insertStudent(student);
            return "success";
        }
        return "filed";
    }
    public Student login(HttpServletResponse response,String number, String studentPassword) {
        if (studentMapper.isStudent(number, studentPassword) != null){
            Cookie autoLoginCookie = new Cookie("studentId", studentMapper.findByNumber(number).getId());
            //cookie的存在时间
            autoLoginCookie.setMaxAge(30 * 60); // 设置Cookie的有效期，单位为秒
            autoLoginCookie.setPath("/"); // 设置Cookie的路径，使其在整个应用程序中都有效
            response.addCookie(autoLoginCookie);
            return studentMapper.isStudent(number, studentPassword);
        }
        return new Student();

    }


    public Student selectStudentInfo(String number){
        return studentMapper.findByNumber(number);
    }
}
